/*
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * "The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * The Original Code is ICEfaces 1.5 open source software code, released
 * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
 * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
 * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 *
 * Contributor(s): _____________________.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
 * License), in which case the provisions of the LGPL License are
 * applicable instead of those above. If you wish to allow use of your
 * version of this file only under the terms of the LGPL License and not to
 * allow others to use your version of this file under the MPL, indicate
 * your decision by deleting the provisions above and replace them with
 * the notice and other provisions required by the LGPL License. If you do
 * not delete the provisions above, a recipient may use your version of
 * this file under either the MPL or the LGPL License."
 *
 */
/*
 * Version: GPL 2.0/LGPL 2.1
 * mooncatventures.com / derived from icefaces auction example
 */
package com.icesoft;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.icesoft.faces.async.render.SessionRenderer;
import com.icesoft.x.demo.ArtShowItem;
import com.mcv.APNSSupport.APNSDevice;
import com.mcv.APNSSupport.APNSDeviceFactory;
import com.mcv.APNSSupport.SendAPNSMessage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.Random;

/**
 * Class used to contain application wide state information Holds a list of all
 * users currently in the chat Can also update everyone in this list by calling
 * reRender on each UserBean This is done in a seperate thread so processing can
 * continue normally
 */
public class AppState {
  
    /**
	 * @return the sessionCount
	 */
	public static synchronized int getSessionCount() {	
		return sessionCount++;

	}


	/**
	 * @param sessionCount the sessionCount to set
	 */
	public  synchronized void setSessionCount(byte[] imageMap, String query) {
		
		
		data.add(new ArtShowItem("Sent by my Iphone",null, null,"2",imageMap));
		
		  SessionRenderer.render("all");
		  String path1 = getCertificateRealPath();
		 SendAPNSMessage apns = new  SendAPNSMessage(getCertificateRealPath(),
				passCode, apnsDeviceFactory.getApnsDevice() );
	}
	
	
	
	private static Log log = LogFactory.getLog(AppState.class);
	private static String passCode ="mypassword";
    private static AppState singleton = null;
    private Random generator = new Random(System.currentTimeMillis());
    static List data = new ArrayList();
	 static boolean stateInit = false;
	 static int sessionCount = 0;
	private static String certificateRealPath = null;
	private static APNSDeviceFactory apnsDeviceFactory; 
   
	  
	    /**
	 * @return the certificateRealPath
	 */
	public static String getCertificateRealPath() {
		return certificateRealPath;
	}


	/**
	 * @param certificateRealPath the certificateRealPath to set
	 */
	public static void setCertificateRealPath(String certificateRealPath) {
		AppState.certificateRealPath = certificateRealPath;
	}


		public  synchronized  void setArt (List art) {
	    	setData(art);
	    	stateInit = true;
	    }
   

    /**
	 * @return the data
	 */
	public static List getData() {
		return data;
	}
	
	 
	/**
	 * @param data the data to set
	 */
	public static void setData(List data) {
		AppState.data = data;
	}

	/**
	 * @return the stateInit
	 */
	public static boolean isStateInit() {
		return stateInit;
	}

	/**
	 * @param stateInit the stateInit to set
	 */
	public static void setStateInit(boolean stateInit) {
		AppState.stateInit = stateInit;
	}

	/**
     * Default constructor with no parameters, is private to fulfill the
     * singleton
     */
    private AppState() {
    }

    public synchronized void addotem (String name, byte[] imBytes) {
  	  data.add(new ArtShowItem(name, "Sent by my Iphone V3.0", null,"2",imBytes));
  	 SessionRenderer.render("all");
  }
    /**
     * Method to return a singleton instance of this class
     *
     * @return this AppState object
     */
    public static synchronized AppState getInstance() {
        if (singleton == null) {
            singleton = new AppState();
            apnsDeviceFactory = new APNSDeviceFactory();
        }
        return (singleton);
    }
    
    public static synchronized void addDevice (String deviceId,String token, String descr) {
    	apnsDeviceFactory.addDevice(deviceId, token, null);
    }
    
 

    
}
